package biblio.DAO;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


@SuppressWarnings("serial")
public class MasterDAOBean<T> implements MasterDAO<T>, Serializable{
	@PersistenceContext(unitName="biblioteca")
	
	private Class<T> pc; //persistenceClass
	protected EntityManager em; //entityManager


	public MasterDAOBean(Class<T> pc) {
		this.pc = pc;
	}
	
	protected EntityManager getEm() {
		return em;
	}

	public void setEm(EntityManager em) {
		this.em = em;
	}
	
	public Class<T> getPc() {
		return pc;
	}	
	
	@Override
	public void save(T entity) {	//salvar
		em.persist(entity);
	}

	@Override
	public T recuparar(int id) {	//recuparar
		return em.find(pc, id);		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> recuperarTudo() {	//recupar tudo
		return em.createQuery("select t from " + pc.getSimpleName() + " t").getResultList();
	}

	@Override
	public void update(T entity) {	//atualizar
		em.merge(entity);
	}

	@Override
	public void remove(T entity) {	//remover
		em.remove(entity);
	}

	@Override
	public void remove(int id) {
		em.remove(em.find(pc, id));
		
	}



	

}
